<?php

namespace App\Http\Controllers\Customer;

use App\Http\Models\Admin\File\FileContent;
use App\Http\Models\Admin\Sys\Contacts;
use App\Http\Responses\ResponseDto;
use App\Services\Admin\EMailService;
use App\Services\Customer\ContentService;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;

class HomeController extends Controller
{

    public function __construct(ContentService $service){
        $this->service = $service;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {//
        $view =  View::make('customer.home')
            ->withAssets($request['ASSETS']);

        $file= FileContent::where('name','Home')->first() ;


        if($file){
            $view->withContent($file->page_content);
        }
        return $view;



    }
    public function waiting(Request $request)
    {
        //
        $view =  View::make('customer.waiting')
            ->withAssets($request['ASSETS']);
        $file= FileContent::where('name','Waiting')->first() ;
        if($file){
            $view->withContent($file->page_content);
        }
        return $view;

    }
    public function categoryshow(Request $request,$category)
    {
        $find = $this->service->getTarget($category);
        $f_child = $this->service->firstChild($category);
        $this->service->buildAssets($category);

        if($find){
            $template = $find->template? $find->template:Config::get("common.default_template");
            $view =  View::make(Config::get("common.default_template_folder").$template)
                ->withAssets($request['ASSETS']);
            $content = $find->page_content;
            if($content == null || $content == null){
                if($f_child){
                    return redirect("/".$category."/".$f_child->name);
                }
            }
            $view->withContent($content);
            return $view;
        }
        else{
            abort(404);
        }
    }
    public function subcategoryshow(Request $request,$category,$subcategory)
    {
        $service_index = 0;
        $services = $request['ASSETS']['footer_service'];


        if($services){
            //$subcategory
            foreach($services  as $index=>$value){
                if(trim($value->name) ==trim($subcategory) ){
                    $service_index = $index;
                }
            }
        }


       $find = $this->getContent($category,$subcategory);
        $this->buildAssets($category,$subcategory);

        if($find && count($find)>0){
            $page = $find[0];
            $template = $page->template? $page->template:Config::get("common.default_template");
            $view =  View::make(Config::get("common.default_template_folder").$template)
                ->withAssets($request['ASSETS']);

            $view->withContent($page->page_content);
            $view->withServiceindex($service_index);
            return $view;
        }
        else{
            abort(404);
        }

    }
    public function getContent($category,$subcategory=null)
    {
//          $join =   DB::select("select t.* ,s.`name` as parentname from file_content t  left join  file_content s  on  t.parent_id = s.id ");

          if($category){
              $find = DB::select("select t.* ,s.`name` as parentname from file_content t  left join  file_content s  on
                          t.parent_id = s.id where t.name =:category ",["category"=>$category]);


          }
          if($subcategory){
              $find = DB::select("select t.* ,s.`name` as parentname from file_content t  left join  file_content s  on
                          t.parent_id = s.id where    s.name =? and t.name=?  ",[$category,$subcategory]);
          }

        return $find;
    }

    public function buildAssets($category,$subcategory=null)
    {

        $base = FileContent::select('id','name','title','parent_id',"page_content")->where('name',$category)->where('parent_id',0)->first();
        //设置子分类
        $childs = FileContent::select('id','name','title')->where('parent_id',$base->id)->orderBy('show_index','asc')->get();
        if($childs){
            foreach($childs as $citem){
                $citem->parent_name = $base->name;
            }
        }
        if($subcategory){
            $base = FileContent::where('name',$subcategory)->where('parent_id',$base->id)->first();
        }else{
            if($base->page_content == null || $base->page_content ==""){
                if($childs && count($childs)>0){
                    $base = $childs[0];
                }
            }
        }

        $assets = request()['ASSETS'];
        $assets['subcategory'] = $childs;
        $assets['currentcategory'] = $base;
        request()['ASSETS'] = $assets;

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function savecontact()
    {
        //
        $validator = Validator::make(Input::all(),
            [
                'username' => 'required',
                'email' => 'required|email',
                'phone' => 'required',
                'message' => 'required',
            ]);
        if($validator->fails()){
            return ResponseDto::createResObj($validator->errors()->first(),ResponseDto::$FAIL);
        }else{
            $contact = new Contacts();
            $contact->username = request()->input('username');
            $contact->email = request()->input('email');
            $contact->message = request()->input('message');
            $contact->phone = request()->input('phone');
            $contact->save();

            $mailservice = new EMailService();
            $mailservice->alHostMail($contact,"check_contacts");

            return ResponseDto::createResObj(trans("custom/message.contact_submit_success"),ResponseDto::$SUCCESS);
        }
    }
}
